﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Cloud.Boss.Models;
using Cloud.Dto;
using Cloud.Framework.Filter;
using Cloud.Service.Log;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Cloud.Boss.Controllers
{
    public class LogController : BaseController
    {
        public LogController(IOptions<ProjectSetting> _projectSetting, ILogService _logService) : base(_projectSetting, _logService)
        {
        }

        [HttpGet][NoLogAction]
        public  IActionResult ReqeustLogManger()
        {
            //int a = 0;
            //a = 3 / a;
            return View();
        }
        [HttpGet]
        [NoLogAction]
        public async Task<IActionResult> ReqeustLogPagedList(int pageIndex = 1,string ControllerName=null,string ActionName=null)
        {
            var list=await _logService.GetRequestLogPageListAsync(pageIndex, _projectSetting.Value.PageSize,ControllerName, ActionName);
            return PagedList(list);
        }
        [HttpGet]
        [NoLogAction]
        public IActionResult ExceptionLogManger()
        {
            return View();
        }
        [HttpGet]
        [NoLogAction]
        public async Task<IActionResult> ExceptionLogPagedList(int pageIndex = 1, string ControllerName = null, string ActionName = null)
        {
            var list = await _logService.GetExceptionLogPageListAsync(pageIndex, _projectSetting.Value.PageSize, ControllerName, ActionName);
            return PagedList(list);
        }

        [HttpPost]
        [NoLogAction]
        [PublicAction]
        [Produces("application/json")]
        //[Route("writeExceptionLog")]
        public void WriteExceptionLog(ExceptionLogDto model)
        {
            try
            {
                if (model.SecretKey==_projectSetting.Value.SecretKey)
                {
                    _logService.WriteExceptionLog(model);
                    return;
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }
}